﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Exam.Core.Model;

public partial class Manage_Course : ManagePageBase {
    protected int cid = 0;

    protected void Page_Load(object sender, EventArgs e) {
        btnSearch.Text = Resources.labels.search;
        if (Request.QueryString["id"]==null){
            Response.End();
        }
        if(!int.TryParse(Request.QueryString["id"], out cid)){
            Response.End();
        }else{
            if(!IsPostBack){
                BindCourse();
                BindType();
                BindDiff();
                BindQuestions();
            }
        }
    }

    private void BindType() {
        StringBuilder sb = new StringBuilder();
        List<Exam.Core.Model.QuestionType> Typelist = new Exam.Core.DAL.QuestionType().getQuestionTypeList();
        foreach (Exam.Core.Model.QuestionType obj in Typelist) {
            QType.Items.Add(new ListItem(obj.QuestionTypeTitle.Trim(), obj.QuestionTypeId.Trim()));
            sb.Append(obj.QuestionTypeId.Trim() + ",");
            QType2.Items.Add(new ListItem(obj.QuestionTypeTitle.Trim(), obj.QuestionTypeId.Trim()));

        }
        QType.Items.Insert(0, new ListItem(Resources.labels.allqtype, sb.ToString().Trim(',')));
    }

    private void BindDiff() {
        StringBuilder sb = new StringBuilder();
        List<Exam.Core.Model.QuestionDifficulty> Difflist = new Exam.Core.DAL.QuestionDifficulty().getQuestionDifficultyList();
        foreach (Exam.Core.Model.QuestionDifficulty obj in Difflist) {
            QDifficulty.Items.Add(new ListItem(obj.QuestionDifficultyTitle.Trim(), obj.QuestionDifficultyId.Trim()));
            sb.Append(obj.QuestionDifficultyId.Trim() + ",");
            QDifficulty2.Items.Add(new ListItem(obj.QuestionDifficultyTitle.Trim(), obj.QuestionDifficultyId.Trim()));
        }
        QDifficulty.Items.Insert(0, new ListItem(Resources.labels.allqdiff, sb.ToString().Trim(',')));

    }


    private void BindCourse() {
        Exam.Core.Model.Course obj = new Exam.Core.DAL.Course().getCourseById(cid);
        cname.Text = obj.Title.Trim();
        StringBuilder sb = new StringBuilder();
        StringBuilder sbk = new StringBuilder();
        
        foreach (Knowledge know in obj.Knowledges) {
            sb.Append(know.Title.Trim());
            sb.Append("，&nbsp;&nbsp;");
            QKnowledge.Items.Add(new ListItem(know.Title.Trim(), know.Id.Trim()));
            sbk.Append(know.Id.Trim() + ",");
            QKnowledge2.Items.Add(new ListItem(know.Title.Trim(), know.Id.Trim()));
        }
        QKnowledge.Items.Insert(0, new ListItem(Resources.labels.allqknowledge, sbk.ToString().Trim(',')));
        //cknow.Text = sb.ToString();
        //AspNetPager1.RecordCount = int.Parse(obj.QuestionCount);

        StringBuilder sbtj = new StringBuilder();
        foreach (QuestionType type in obj.QuestionTypes) {
            sbtj.Append(type.QuestionTypeTitle.Trim() + " <b>" + type.QuestionCount + "</b> ");
        }
        string tmp = Resources.labels.course_know_question;
        tmp = string.Format(tmp, obj.Knowledges.Count, obj.QuestionCount.Trim(), sbtj.ToString());
        LabTj.Text = tmp;//"该课程共有 <b>" + obj.Knowledges.Count + "</b>个知识点，<b>" + obj.QuestionCount.Trim() + "</b>道试题. " + sbtj.ToString() + "";


        

        
    }

    private void BindQuestions() {
        

        string QuestionTypeId = this.QType.SelectedValue.Trim(',');
        string QuestionDifficultyId = this.QDifficulty.SelectedValue.Trim(',');
        string QuestionKnowledgeId = this.QKnowledge.SelectedValue.Trim(','); 
        string CourseId = cid.ToString(); 
        string IsShow="0";
        string keyword = this.keywordTb.Text.Trim();
        int pagesize = AspNetPager1.PageSize;
        int page = AspNetPager1.CurrentPageIndex-1; //GetPageIndex();
        int size = new Exam.Core.DAL.Question().getSize(
                QuestionTypeId,
                QuestionDifficultyId,
                QuestionKnowledgeId,
                CourseId,
                IsShow,
                keyword);
        AspNetPager1.RecordCount = size;
        

        List<Exam.Core.Model.Question> list = new Exam.Core.DAL.Question().getQuestionListByPage(
                pagesize,
                page,
                QuestionTypeId, 
                QuestionDifficultyId, 
                QuestionKnowledgeId, 
                CourseId, 
                IsShow, 
                keyword
            );
        rp.DataSource = list;
        rp.DataBind();
    }

    protected string getQtxt(string str)
    {
        return HttpUtility.HtmlDecode(str);
    }

    protected string getQType(string id){
        string title="";
        foreach (ListItem item in QType.Items) {
            if(id.Trim()==item.Value.Trim()) {
                title = item.Text.Trim();
                break;
            }
        }
        return title.Trim();
    }
    protected string getQDiff(string id){
        string title = "";
        foreach (ListItem item in QDifficulty.Items) {
            if (id.Trim() == item.Value.Trim()) {
                title = item.Text.Trim();
                break;
            }
        }
        return title.Trim();
    }

    private int GetPageIndex() {
        int index = 0;
        if (int.TryParse(Request.QueryString["page"], out index))
            index--;
        return index;
    }


    protected void btnSearch_Click(object sender, EventArgs e) {
        //BindCourse();
        //BindType();
        //BindDiff();
        BindQuestions();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e) {
        //BindCourse();
        //BindType();
        //BindDiff();
        BindQuestions();
    }
    protected string getAnswer(string str) {
        if (str == "TRUE")
            return Resources.labels.right;
        if (str == "FALSE")
            return Resources.labels.error;
        return str;
    }
}
